package com.gateway.security.extract;

import com.gateway.common.enums.AuthenticationTypeEnum;
import com.gateway.common.enums.AuthorizationTypeEnum;
import com.gateway.plugin.security.oauth2.extract.OAuth2Extractor;
import com.gateway.security.common.authentication.client.ClientAuthenticationToken;
import com.gateway.security.common.extract.ClientExtractor;
import com.gateway.security.common.extract.DefaultClientExtractor;
import com.gateway.plugin.security.uaa.extract.BasicExtractor;
import com.gateway.plugin.security.uaa.extract.MobileApplicationExtractor;
import com.google.common.collect.Maps;
import org.springframework.web.server.ServerWebExchange;

import java.util.Map;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/12 22:47
 **/
public class ClientExtractorFactory {
    private static final Map<String, ClientExtractor> extractorMap;

    static {
        extractorMap = Maps.newHashMapWithExpectedSize(AuthorizationTypeEnum.values().length);
        extractorMap.put(AuthenticationTypeEnum.BASIC.getType(), new BasicExtractor());
        extractorMap.put(AuthenticationTypeEnum.MOBILE_APPLICATION.getType(), new MobileApplicationExtractor());
        extractorMap.put(AuthenticationTypeEnum.OAUTH2.getType(), new OAuth2Extractor());
    }

    private ClientExtractorFactory() {
    }

    /**
     * New instance ClientExtractor
     *
     * @param type the Authentication type
     * @return the ClientExtractor
     */
    public static ClientExtractor newInstance(final String type) {
        return extractorMap.getOrDefault(type, DefaultClientExtractor.getInstance());
    }

    public static ServerWebExchange extract(String type, ServerWebExchange exchange, ClientAuthenticationToken token) {
        return newInstance(type).extract(exchange, token);
    }
}
